<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{height: mainHeight+'px'}">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                    </div>
                    <div class="bh-btn-group">
                        <div class="bh-search">
                            <el-input  size="medium" placeholder="请输入订单号" v-model="search.oid" class="input-with-select">
                                <el-button slot="append" icon="el-icon-search" @click="list"></el-button>
                            </el-input>
                        </div>
                        <div class="bh-search"  style="width: 140px">
                            <el-select size="medium" v-model="search.status" @change="changeSearchStatus" placeholder="状态">
                                <el-option label="状态(全部)" :value="null"></el-option>
                                <el-option label="未付款" :value="1"></el-option>
                                <el-option label="待发货" :value="2"></el-option>
                                <el-option label="已发货" :value="3"></el-option>
                            </el-select>
                        </div>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              :data="tableData"
                              :border="true" style="width: 100%">
                        <el-table-column prop="id" label="编号" width="60px"></el-table-column>
                        <el-table-column prop="oid" label="订单号" width="150px"></el-table-column>
                        <el-table-column prop="mall_thumb" label="封面图" width="120px">
                            <template slot-scope="scope">
                                <img :src="scope.row.mall_thumb" style="height: 55px;"/>
                            </template>
                        </el-table-column>
                        <el-table-column prop="mall_name" label="商品名称"></el-table-column>
                        <el-table-column prop="number" label="兑换数量" width="100px"></el-table-column>
                        <el-table-column prop="score" label="消耗积分" width="100px"></el-table-column>
                        <el-table-column prop="user_name" label="收件人名" width="100px"></el-table-column>
                        <el-table-column prop="user_address" label="收件地址" width="200px"></el-table-column>
                        <el-table-column prop="user_phone" label="收件电话" width="150px"></el-table-column>
                        <el-table-column prop="created_at" label="创建时间" width="200px"></el-table-column>
                        <el-table-column prop="updated_at" label="更新时间" width="200px"></el-table-column>
                        <el-table-column prop="status_text" label="状态" width="80px">
                            <template slot-scope="scope">
                                <span v-html="scope.row.status_text"></span>
                            </template>
                        </el-table-column>

                        <el-table-column label="操作" width="100px">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="changeStatus(scope.row)" icon="fa fa-edit" plain>
                                    发货</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <div class="pagination-total">共 <em>{{pageData.total}}</em> 个订单</div>
                        <el-pagination  v-if="!loading && pageData && pageData.last_page > 1" background layout="prev, pager, next, jumper"
                                        :page-size="pageData.per_page"
                                        :total="pageData.total" :current-page="pageData.current_page"
                                        @current-change="handleCurrentChange">
                        </el-pagination>
                    </div>
                </div>
            </section>
        </el-main>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {OrderList,OrderUpdate} from '../../utils/axios';
    import { VueCropper }  from 'vue-cropper';
    export default {
        components:{vBreadcrumb,VueCropper},
        data(){
            return {
                breadcumb:[{name: "订单管理"}],
                updateDialogTile:'',
                tableData:[],//列表数据
                pageData:[],//分页数据
                formData:{},
                updateDialog:false,
                search: {oid: null,status:null},//筛选
                previews: {},
                option: {
                    size: 1,
                    full: false,
                    outputType: 'png',
                    canMove: true,
                    fixedBox: true,
                    autoCrop: true,
                    autoCropWidth: 360,
                    autoCropHeight: 204,
                    original: false,
                    canMoveBox: true
                },
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/order');
            this.list();
            this.previews = {};
        },
        computed: {
            ...mapGetters(['active','mainHeight','loading','upload']),
            myHeaders(){
                var token = this.$storage.get('token');
                return {
                    Authorization:token.token_type+' ' + token.access_token,
                    Accept:'application/json'
                }
            },
        },
        methods:{
            //分页
            handleCurrentChange(val) {
                this.search.page = val;
                this.list();
            },
            //获取列表
            list(){
                this.$store.dispatch('setLoading', true);
                OrderList({params: this.search}).then((response)=>{
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data.data;
                        this.pageData = response.data;
                    }
                })
            },
            //新增
            // create(){
            //     this.formData = {id:'',name: '', thumb: '', prices: 1,score:1,number:1,intro:'',details:[],status:1,changed:0,sort:0};
            //     this.updateDialog = true;
            //     this.updateDialogTile = '新增商品';
            //     this.fileList = [];
            // },
            //编辑  prices: 0,score:0,number:0,intro:'',details:0,status:0,changed:0,sort:0
            changeStatus(info){
                var _this = this;
                _this.formData = {
                    id : info.id,
                    status : 3
                };
                this.update();

            },
            //更新
            update(){
                OrderUpdate(this.formData).then((response)=>{
                    if(response.code){
                        this.$message.error(response.msg);
                    }else{
                        this.$message({
                            type: 'success',
                            duration: '1000',
                            message: response.msg,
                            onClose:() =>{
                                this.updateDialog = false;
                                this.list();
                            }
                        });
                    }
                });
            },
            //更新状态
            changeSearchStatus(){
                this.list();
            }
        },
        filters: {
            channel: function (val,data) {
                if (!val) return '';
                let name = '';
                data.filter((item)=>{
                    if(item.id === val){
                        name = item.name;
                    }
                });
                return name;
            }
        }
    }
</script>
